package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.ClearCartItemRequest;
import com.cskaoyan.mall.dto.ClearCartItemResponse;
import com.cskaoyan.shopping.converter.CartItemConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.ICartService;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * @auther cskaoyan
 * @date 2022/6/16:16:34
 */
@Service
public class ICartSeviceImpl implements ICartService {

    @Autowired
    RedissonClient redissonClient;

    @Autowired
    ItemMapper itemMapper;

    @Override
    public CartListByIdResponse getCartListById(CartListByIdRequest request) {
        ArrayList<CartProductDto> cartProductDtos = new ArrayList<>();
        CartListByIdResponse response = new CartListByIdResponse();
        RMap<Object, Object> cartProducts = redissonClient.getMap(request.getUserId().toString());
        try {
            // 参数校验
            request.requestCheck();
            Collection<Object> values = cartProducts.values();

            for (Object value : values) {
                cartProductDtos.add((CartProductDto) value);
            }
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            response.setCartProductDtos(cartProductDtos);
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }

    @Override
    public AddCartResponse addToCart(AddCartRequest request) {
        RMap<Object, Object> cartProducts = redissonClient.getMap(request.getUserId().toString());
        AddCartResponse response = new AddCartResponse();
        try {
            CartProductDto cartProductDto = null;
            // 参数校验
            request.requestCheck();
            if (cartProducts.containsKey(request.getItemId())) {
                cartProductDto = (CartProductDto) cartProducts.get(request.getItemId());
                cartProductDto.setProductNum(cartProductDto.getProductNum() + request.getNum());
            } else {
                Item item = itemMapper.selectByPrimaryKey(request.getItemId());
                cartProductDto = CartItemConverter.item2Dto(item);
                cartProductDto.setProductNum(Long.valueOf(request.getNum()));
            }
            cartProductDto.setChecked("true");
            cartProducts.put(request.getItemId(), cartProductDto);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }

    @Override
    public UpdateCartNumResponse updateCartNum(UpdateCartNumRequest request) {
        RMap<Object, Object> cartProducts = redissonClient.getMap(request.getUserId().toString());
        UpdateCartNumResponse response = new UpdateCartNumResponse();
        try {
            // 参数校验
            request.requestCheck();
            CartProductDto cartProductDto = (CartProductDto) cartProducts.get(request.getItemId());
            cartProductDto.setProductNum(Long.valueOf(request.getNum()));
            cartProductDto.setChecked(request.getChecked());
            cartProducts.put(request.getItemId(), cartProductDto);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }

    @Override
    public CheckAllItemResponse checkAllCartItem(CheckAllItemRequest request) {
        RMap<Object, Object> map = redissonClient.getMap(request.getUserId().toString());
        // Collection<Object> products = map.values();
        Set<Object> keys = map.keySet();
        for (Object key : keys) {
            CartProductDto cartProductDto = (CartProductDto)map.get(key);
            cartProductDto.setChecked(request.getChecked());
            map.put(key,cartProductDto);
            //System.out.println(((CartProductDto)map.get(key)).getChecked());
        }
        CheckAllItemResponse checkAllItemResponse = new CheckAllItemResponse();
        try {
            // 参数校验
            request.requestCheck();
            checkAllItemResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            checkAllItemResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(checkAllItemResponse, e);
        }
        return checkAllItemResponse;
    }

    @Override
    public DeleteCartItemResponse deleteCartItem(DeleteCartItemRequest request) {
        RMap<Object, Object> cartProducts = redissonClient.getMap(request.getUserId().toString());
        DeleteCartItemResponse response = new DeleteCartItemResponse();
        try {
            // 参数校验
            request.requestCheck();
            Object remove = cartProducts.remove(request.getItemId());
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }

    /**
     * WANGJun
     * @param request
     * @return
     */
    @Override
    public DeleteCheckedItemResposne deleteCheckedItem(DeleteCheckedItemRequest request) {
        DeleteCheckedItemResposne checkedItemResposne = new DeleteCheckedItemResposne();
        try {
            // 参数校验
            request.requestCheck();
            RMap<Object, Object> map = redissonClient.getMap(request.getUserId().toString());
            // Collection<Object> products = map.values();
            Set<Object> keys = map.keySet();
            for (Object key : keys) {
                if ( "true".equals(((CartProductDto)map.get(key)).getChecked())){
                    map.remove(key);
                }
            }
            checkedItemResposne.setCode(ShoppingRetCode.SUCCESS.getCode());
            checkedItemResposne.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(checkedItemResposne, e);
        }
        return checkedItemResposne;
    }

    @Override
    public ClearCartItemResponse clearCartItemByUserID(ClearCartItemRequest request) {
        ClearCartItemResponse response = new ClearCartItemResponse();
        System.out.println("清空购物车启动成功");
        try {
            // 参数校验
            request.requestCheck();
            RMap<Object, Object> map = redissonClient.getMap(request.getUserId().toString());
            // Collection<Object> products = map.values();
            Set<Object> keys = map.keySet();
            for (Object key : keys) {
                // System.out.println("+++++++++"+request.getProductIds().toString());
                // if (request.getProductIds().contains(key)){
                //
                //     map.remove(key);
                // }
                if ( "true".equals(((CartProductDto)map.get(key)).getChecked())){
                    //System.out.println("key find");
                    map.remove(key);
                }
            }
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }
}
